package com.eascs.web.o2o.report.dao;

import com.eascs.app.dao.JdbcSession;
import com.eascs.app.dao.PageResult;
import com.eascs.app.dao.SqlPageQuery;
import com.eascs.app.runtime.RuntimeSetting;
import com.eascs.common.util.StringUtil;
import com.eascs.web.o2o.report.constants.FefundConstants;
import com.eascs.web.o2o.report.entity.O2OMessage;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import javax.inject.Inject;
import java.util.List;

@Repository
public class O2OMessageDao {
	@Inject
	JdbcSession jdbcSessionForService;
	@Inject
	JdbcSession jdbcSession;
	@Inject
	JdbcSession jdbcSessionForSlaveService;
	/**
	 * 分页查询
	 * 
	 * @param sqlPageQuery
	 * @return
	 */
	public <T> PageResult<T> findDataWithPage(SqlPageQuery<T> sqlPageQuery) {
		RuntimeSetting runtimeSetting = RuntimeSetting.get();
		if(runtimeSetting.getEnv().equals(FefundConstants.ENV_PROD)){
			return jdbcSessionForSlaveService.findDataWithPage(sqlPageQuery);//只有生产环境有主从隔离（从库只读）
		}
		return this.jdbcSessionForService.findDataWithPage(sqlPageQuery);
	}
	
	public List<Object[]> findParams(String sql,Object...params ){
		RuntimeSetting runtimeSetting = RuntimeSetting.get();
		if(runtimeSetting.getEnv().equals(FefundConstants.ENV_PROD)){
			return jdbcSessionForSlaveService.findObjectsList(sql, params);//只有生产环境有主从隔离（从库只读）
		}
		return this.jdbcSessionForService.findObjectsList(sql, params);
	}
	
	
    @Transactional(transactionManager = "transactionManagerForService")
	public int excuteSql(String sql, Object... params) {
	    return jdbcSessionForService.execute(sql, params);
	}
	
	/**
	 * s 保存该表数据
	 */
	@Transactional(transactionManager = "transactionManagerForService")
	public void saveEntity(Object entity) {
		jdbcSessionForService.insert(entity);
	}
	
	public <T> List<T> findEntitys(String sql, Class<T> requiredType, Object... params) {
		RuntimeSetting runtimeSetting = RuntimeSetting.get();
		if(runtimeSetting.getEnv().equals(FefundConstants.ENV_PROD)){
			return jdbcSessionForSlaveService.findEntitys(sql, requiredType, params);//只有生产环境有主从隔离（从库只读）
		}
		return jdbcSessionForService.findEntitys(sql, requiredType, params);
	}

	public <T> T findEntity(String sql, Class<T> requiredType, Object... params) {
		RuntimeSetting runtimeSetting = RuntimeSetting.get();
		if(runtimeSetting.getEnv().equals(FefundConstants.ENV_PROD)){
			return jdbcSessionForSlaveService.findEntity(sql, requiredType, params);//只有生产环境有主从隔离（从库只读）
		}
		return jdbcSessionForService.findEntity(sql, requiredType, params);
	}
	
	public boolean isExist(String type,String relevanceId){
		StringBuilder checksql = new StringBuilder();
		checksql.append("select * from o2o_message where 1=1 ");
		if(StringUtil.isNotBlank(type)){
			checksql.append(" and message_type = ? ");
		}
		if(StringUtil.isNotBlank(relevanceId)){
			checksql.append(" and relevance_id = ? ");
		}
		List<O2OMessage> list = null;
		RuntimeSetting runtimeSetting = RuntimeSetting.get();
		if(runtimeSetting.getEnv().equals(FefundConstants.ENV_PROD)){
			list= jdbcSessionForSlaveService.findEntitys(checksql.toString(), O2OMessage.class,type,relevanceId);//只有生产环境有主从隔离（从库只读）
		}else{
			list = jdbcSessionForService.findEntitys(checksql.toString(), O2OMessage.class,type,relevanceId);
		}
		if(list.size()>0){
			return true;
		}else{
			return false;
		}
	}
	
	public boolean updateRelevanceId(String relevanceId,String messageId,String status){
		StringBuilder checksql = new StringBuilder();
		checksql.append("update o2o_message set wf_message_id = ?,status = ? where message_id = ?  ");
	    int rs = excuteSql(checksql.toString(),relevanceId,status,messageId);
	    if(rs>0){
	    	return true;
	    }else{
	    	return false;
	    }
	}
	/**
	 * 分页查询
	 * 
	 * @param sqlPageQuery
	 * @return
	 */
	public <T> PageResult<T> findDataWithPageByDev(SqlPageQuery<T> sqlPageQuery) {
		return this.jdbcSession.findDataWithPage(sqlPageQuery);
	}
	/**
	 * service数据源
	 * @param sqlPageQuery
	 * @return
	 */
	public <T> PageResult<T> findDataWithPageByService(SqlPageQuery<T> sqlPageQuery) {
		return this.jdbcSessionForService.findDataWithPage(sqlPageQuery);
	}
	
	public <T> T findEntityDev(String sql, Class<T> requiredType, Object... params) {

		return jdbcSession.findEntity(sql, requiredType, params);
	}
}
